using System;
using System.Data;
using Firestarter.DB.Schema.Test.Logic;
using FireStarterModeller.BO;
using Firestarter.DB.Schema.Logic;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.DB;
using Habanero.Testability;

// ReSharper disable VirtualMemberNeverOverriden.Global
namespace Firestarter.DB.Schema.Test.Logic
{
    public abstract class TestUtilsDB
    {
        private string _testDatabaseName = "Schema_Test" + RandomValueGen.GetRandomString(5);

        public virtual string TestDatabaseName
        {
            get { return _testDatabaseName; }
            set { _testDatabaseName = value; }
        }

        public abstract DatabaseConfig CreateTestDatabaseConfig();
        protected abstract DatabaseConfig CreateMasterDatabaseConfig();
        public virtual IDbConnection CreateTestDBConnection()
        {
            DatabaseConfig databaseConfig = CreateTestDatabaseConfig();
            IDatabaseConnection databaseConnection = databaseConfig.GetDatabaseConnection();
            return databaseConnection.GetConnection();
        }
        public virtual IDbConnection CreateMasterConnection()
        {
            DatabaseConfig databaseConfig = CreateMasterDatabaseConfig();
            IDatabaseConnection databaseConnection = databaseConfig.GetDatabaseConnection();
            return databaseConnection.GetConnection();
        }
        public abstract CreateTableSqlBuilder CreateSqlBuilderCreateTable(string tableName);
        public abstract void CreateTestDatabase();
        /// <summary>
        /// Removes all data from the <see cref="TestDatabaseName"/>.
        /// </summary>
        /// <param name="connection"></param>
        public abstract void ClearDatabase(IDbConnection connection);

        /// <summary>
        /// Drop the <see cref="TestDatabaseName"/>.
        /// </summary>
        public abstract void DropDatabase();
        public virtual void ExecuteSql(IDbConnection connection, string sql)
        {
            OpenConnection(connection);
            IDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            command.ExecuteNonQuery();
        }
        [CoverageExclude]
        protected static void OpenConnection(IDbConnection connection)
        {
            try
            {
                if (connection.State == ConnectionState.Closed) connection.Open();
            }
            catch (Exception e)
            {
                throw new HabaneroApplicationException("There was an error trying to open the connection '" + connection.ConnectionString, e);
            }
        }
/*
        public DatabaseConnectionInfo CreateTestDatabaseConnectionInfo()
        {
            DatabaseConfig databaseConfig = CreateTestDatabaseConfig();
            return DatabaseConnectionInfo.CreateFromDatabaseConfig(databaseConfig);
        }*/

        internal SchemaTestPack CreateSchemaTestPack() {
            return SchemaTestPackBuilder.SetupDatabaseSchemaTestPack(this, CreateTestDBConnection(), TestDatabaseName);
        }
/*        public string GetDBSpecificType(string generalisedType)
        {
            return null;
        }

        public string GetGeneralisedType(string dbSpecificType)
        {
            return null;
        }*/

    }


    //internal class SchemaTestDbUtilMSAccess : TestUtilsDB
    //{
    //    public override DatabaseConfig CreateTestDatabaseConfig()
    //    {
    //        return new DatabaseConfig("MSAccess", "localhost", TestDatabaseName, "sa", "sa", null);
    //    }

    //    public override DatabaseConfig CreateMasterDatabaseConfig()
    //    {
    //        throw new System.NotImplementedException();
    //    }

    //    public override CreateTableSqlBuilder_MySql CreateSqlBuilderCreateTable(string tableName)
    //    {
    //        throw new System.NotImplementedException();
    //    }

    //    public override void CreateTestDatabase()
    //    {
    //        try
    //        {
    //            IDbConnection connection = CreateMasterDBConnection();
    //            if (connection.State == ConnectionState.Closed) connection.Open();
    //            IDbCommand command = connection.CreateCommand();
    //            try
    //            {
    //                ClearDatabase(connection);
    //            }
    //            catch (Exception ex)
    //            {
    //                Console.WriteLine(ex);
    //            }

    //            command.CommandText = string.Format(@"CREATE DATABASE [{0}]", TestDatabaseName);
    //            command.ExecuteNonQuery();
    //        }
    //        catch (Exception ex)
    //        {
    //            Console.WriteLine(ex);
    //        }
    //    }

    //    public override void ClearDatabase(IDbConnection connection)
    //    {
    //        throw new System.NotImplementedException();
    //    }
    //}

}
// ReSharper restore VirtualMemberNeverOverriden.Global
